# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution(object):
    def __init__(self):
        self.tree = None

    def searchBST(self, root, val):
        """
        :type root: Optional[TreeNode]
        :type val: int
        :rtype: Optional[TreeNode]
        """
        if not root:
            return self.tree

        return self.loop(root, val)

    def loop(self, node, val):
        if not node:
            return
        if node.val == val:
            self.tree = node
            return self.tree

        if self.loop(node.left, val):
            return self.tree
        if self.loop(node.right, val):
            return self.tree

